package com.taptech.doufu.ui.view.choice.adapter;

import android.content.Context;
import android.graphics.Color;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.constant.UmengEventName;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.bean.choice.NovelModel;
import com.taptech.doufu.ui.view.choice.ChoiceUtils;

import java.util.List;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class ChoiceTopicRankAdapter extends RecyclerView.Adapter<ChoiceTopicRankAdapter.ViewHolder>{

    private List<NovelModel> list;
    private Context context;
    private EventBean eventData;


    public ChoiceTopicRankAdapter(Context context, List<NovelModel> list,EventBean eventData){
        this.list = list;
        this.context = context;
        this.eventData = eventData;
    }

    public NovelModel getItem(int position){
        if (list != null && list.size()>position) {
            return list.get(position);
        }
        return null;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.topic_choice_rank_item, null);
        ViewHolder holder = new ViewHolder(view);
        holder.tvTitle =  view.findViewById(R.id.tvTitle);
        holder.tvRank = view.findViewById(R.id.tvRank);
        holder.tvAuthor =  view.findViewById(R.id.tvAuthor);
        holder.ivPic =  view.findViewById(R.id.ivPic);
        holder.lay = view.findViewById(R.id.lay);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final NovelModel model = getItem(position);
        if (model != null) {
            holder.tvTitle.setText(model.getTitle());
            if (model.getUser() != null) {
                holder.tvAuthor.setText(model.getUser().getNickname());
            }
            holder.tvRank.setText(String.valueOf(position+1));
            if (position > 2) {
                if (WeMediaApplication.getInstance().isDayNightMode_Night) {
                    holder.tvRank.setTextColor(context.getResources().getColor(R.color.text_dark));
                }else{
                    holder.tvRank.setTextColor(context.getResources().getColor(R.color.text_color_22));
                }
            }else{
                holder.tvRank.setTextColor(Color.parseColor("#ECAF00"));
            }
            ChoiceUtils.displayImage(context,model.getCover(),holder.ivPic);
            changeDayModelBg(holder,position);
            holder.lay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ChoiceUtils.addEvent(context,UmengEventName.JINGXUAN_CARD_CONTENT,eventData);
                    ChoiceUtils.jumpToNovelDetail(context,model.getId());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView tvTitle;
        ImageView ivPic;
        TextView tvAuthor;
        TextView tvRank;
        View lay;
        public ViewHolder(View itemView) {
            super(itemView);

        }
    }

    private void changeDayModelBg(ViewHolder viewHolder,int position){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg(viewHolder,position);
        }else {
            dayBg(viewHolder,position);
        }
    }

    private void dayBg(ViewHolder viewHolder,int position) {
        if (position > 2) {
            viewHolder.tvRank.setTextColor(context.getResources().getColor(R.color.text_color_22));
        }else{
            viewHolder.tvRank.setTextColor(Color.parseColor("#ECAF00"));
        }
        viewHolder.lay.setBackgroundColor(context.getResources().getColor(R.color.white));
        viewHolder.tvTitle.setTextColor(context.getResources().getColor(R.color.text_color_22));
        viewHolder.ivPic.setAlpha(1.0f);
    }

    private void darkBg(ViewHolder viewHolder,int position) {
        if (position > 2) {
            viewHolder.tvRank.setTextColor(context.getResources().getColor(R.color.text_dark));
        }else{
            viewHolder.tvRank.setTextColor(Color.parseColor("#ECAF00"));
        }
        viewHolder.lay.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        viewHolder.tvTitle.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        viewHolder.ivPic.setAlpha(Constant.imagViewAlpha);
    }
}
